import axios from "axios";
import Nprogress from 'nprogress'

// 引入加载的样式
import 'nprogress/nprogress.css'

const mockajax = axios.create({
  // 这里已经匹配了基础路径 '/api'
  baseURL: '/mock',
  timeout: 5000,
})

// 请求拦截器和响应拦截器


mockajax.interceptors.request.use(function (config) {

  Nprogress.start()
  return config
})

// 添加响应拦截器
mockajax.interceptors.response.use(
  function (response) {

    Nprogress.done()
    return response.data
  },
  function (error) {

    Nprogress.done()
    console.log(error)

    let status = error.response.status
    if (status === 401) {
      alert('未授权')
    } else if (status === 403) {
      alert('禁止访问')
    } else if (status === 404) {
      alert('页面找不到')
    } else if (status === 500) {
      alert('服务器内部错误')
    }

    return new Promise(() => {})
  }
)

export default mockajax